//
//  BaseTableViewController.m
//  DemoProject
//
//  Created by hao on 2018/7/12.
//  Copyright © 2018年 hao. All rights reserved.
//

#import "BaseTableViewController.h"
#import "SettingCell.h"
#import "SettingGroupModel.h"

@interface BaseTableViewController ()

@property (nonatomic, assign) UITableViewStyle tableViewStyle;

@property (nonatomic, strong) NSMutableArray<SettingGroupModel *> *groupModelArr;

@end

@implementation BaseTableViewController

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    if (self = [super init]) {
        _tableViewStyle = style;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableViewStyle = UITableViewStyleGrouped;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupTableView];
}

- (void)setupTableView
{
    self.tableView.backgroundColor = self.view.backgroundColor;
}

- (void)addTitles:(NSArray<NSString *> *)titles footDescs:(NSArray<NSString *> *)footDescs
{
    NSInteger count = titles.count <= footDescs.count ? titles.count : footDescs.count;
    for (int i = 0; i < count; i++) {
        SettingItemModel *item = [[SettingItemModel alloc] init];
        item.title = titles[i];
        item.cellDefaultMargin = 15;
        item.hiddenArrow = YES;
        SettingGroupModel *group = [SettingGroupModel settingGroupModelWithItems:@[item]];
//        group.groupFootHeight = [footDescs[i] sizeForFont:[UIFont systemFontOfSize:17] size:CGSizeMake(self.tableView.width, self.tableView.height) mode:NSLineBreakByWordWrapping].height;
        group.groupFootTitle = footDescs[i];
        [self.groupModelArr addObject:group];
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupModelArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupModelArr[section].itemsArr.count;
}

- (SettingCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingItemModel *itemModel = self.groupModelArr[indexPath.section].itemsArr[indexPath.row];
    SettingCell *cell = [SettingCell cellWithTableView:tableView indexPath:indexPath itemModel:itemModel];
    cell.backgroundColor = [UIColor whiteColor];
    if ([self respondsToSelector:@selector(customStyleWithCell:indexPath:)]) {
        [self customStyleWithCell:cell indexPath:indexPath];
    }
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return self.groupModelArr[indexPath.section].itemsArr[indexPath.row].rowHeight;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section >= self.groupModelArr.count) {
        return 0.00000001;
    }
    return self.groupModelArr[section].groupHeadHeight;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return self.groupModelArr[section].groupFootHeight;
//}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    return self.groupModelArr[section].groupHeadView;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    return self.groupModelArr[section].groupFootView;
//}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return self.groupModelArr[section].groupHeadTitle;
//}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section >= self.groupModelArr.count) {
        return nil;
    }
    return self.groupModelArr[section].groupFootTitle;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:self.tableViewStyle];
        [self.view addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.top = kStatusBarAndNaviHeight();
        tableView.height = self.view.height - tableView.top;
//        if (!self.tabBarController.tabBar.isHidden) {
//            tableView.height -= kTabBarHeight();
//        }
        _tableView = tableView;
    }
    return _tableView;
}

- (UIView *)tableHeadView
{
    if (!_tableHeadView) {
        UIView *_headView = [[UIView alloc] init];
        _headView.backgroundColor = [UIColor clearColor];
        CGFloat widthHeight = self.tableView.width;
        _headView.frame = CGRectMake(0, 0, widthHeight, widthHeight);
        self.tableView.tableHeaderView = _headView;
        _tableHeadView = _headView;
    }
    return _tableHeadView;
}

- (NSMutableArray<SettingGroupModel *> *)groupModelArr
{
    if (!_groupModelArr) {
        _groupModelArr = [NSMutableArray array];
    }
    return _groupModelArr;
}

@end
